/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-09-02
 * Time: 21:10
 */



public class Test {

    // 链表的回文串
    public boolean chkPalindrome(ListNode A) {
        // write code here
        ListNode fast = A;
        ListNode slow = A;
        // 找中间节点
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        // 逆置
        ListNode cur = slow.next;
        slow.next = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        // 判断
        fast = A;
        while(fast != slow) {

            if(fast.val != slow.val) {
                return false;
            }else {
                if(fast.next == slow) {
                    return true;
                }
                fast = fast.next;
                slow = slow.next;
            }
        }
        return true;
    }
    // 分割链表
    public ListNode partition(ListNode pHead, int x) {
        // write code her
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while (cur != null) {
            if (cur.val < x) {
                if (bs != null) {
                    be.next = cur;
                    be = cur;
                } else {
                    bs = cur;
                    be = cur;
                }
            } else {
                if (as != null) {
                    ae.next = cur;
                    ae = cur;
                } else {
                    as = cur;
                    ae = cur;
                }

            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }
    // 求链表的相交节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null && headB != null) {
            return null;
        }
        if(headA != null && headB == null) {
            return null;
        }
        if(headA == null && headB == null) {
            return null;
        }
        ListNode pLong = headA;
        ListNode pShort = headB;
        int len1 = 0;
        int len2 = 0;
        while(pLong != null) {
            len1++;
            pLong = pLong.next;
        }
        while(pShort != null) {
            len2++;
            pShort = pShort.next;
        }
        pLong = headA;
        pShort = headB;
        // 走差值步
        int len = len1 - len2;
        if(len < 0) {
            pLong = headB;
            pShort = headA;
            len = -len;
        }
        while(len != 0) {
            pLong = pLong.next;
            len--;
        }
        // 然后一起走
        while(pLong != pShort) {
            pLong = pLong.next;
            pShort = pShort.next;
        }
        return pLong;
    }
    // 判断链表带环
    public boolean hasCycle(ListNode head) {
        if(head == null) {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
    // 求环的入口点
    public ListNode detectCycle(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public static void main(String[] args) {

    }
}
